package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.School;
import cn.wolfcode.education.domain.Speech;
import cn.wolfcode.education.query.SchoolQuery;
import cn.wolfcode.education.service.ISchoolService;
import cn.wolfcode.education.service.ISpeechService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private ISpeechService speechService;

    @RequestMapping("")
    public String index() {
        return "school/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(SchoolQuery qo) {
        return schoolService.queryForList(qo);
    }

    @RequestMapping("/getSpeechListBySchoolId")
    @ResponseBody
    public PageResult getSpeechListBySchoolId(Long schoolId) {
        return speechService.getSpeechListBySchoolId(schoolId);
    }

    @RequestMapping("/getSchoolById")
    @ResponseBody
    public School getSchoolById(Long id) {
        return schoolService.getSchoolById(id);
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONResult save(School school) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.insert(school);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }


    @RequestMapping("getAllSchool")
    @ResponseBody
    public List<School> getAllSchool() {
        return schoolService.getAllSchool();
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(School school) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.update(school);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 大客户跟单设置
     *
     * @param school
     * @return
     */
    @RequestMapping("/trackSchool")
    @ResponseBody
    public JSONResult trackSchool(School school) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.trackSchool(school);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 丢单
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 签约
     *
     * @param id
     * @return
     */
    @RequestMapping("/signing")
    @ResponseBody
    public JSONResult signing(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.signing(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 合作
     *
     * @param id
     * @return
     */
    @RequestMapping("/cooperation")
    @ResponseBody
    public JSONResult cooperation(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            schoolService.cooperation(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    //添加演讲
    @RequestMapping("/speechSave")
    @ResponseBody
    public JSONResult speechSave(Speech speech) {
        JSONResult jsonResult = new JSONResult();
        try {
            speechService.insertSpeech(speech);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    //
    @RequestMapping("/download")
    @ResponseBody
    public void download(SchoolQuery qo, HttpServletResponse resp) throws Exception {
        qo.setRows(100);
        List<School> rows = schoolService.queryForList(qo).getRows();

        resp.setHeader("Content-Disposition", "attachment;filename=schoolClient.xls");
        OutputStream outputStream = resp.getOutputStream();

        //创建工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //处理标题
        //创建sheet
        WritableSheet sheet = workbook.createSheet("schoolClient", 0);
        //创建单元格
        Label idLabel = new Label(0, 0, "编号");
        Label nameLabel = new Label(1, 0, "学校");
        Label shcLabel = new Label(2, 0, "学校类型");
        Label addressLabel = new Label(3, 0, "地址");
        Label improtanceLabel = new Label(4, 0, "重要度");
        Label washLabel = new Label(5, 0, "意向度");
        Label telLabel = new Label(6, 0, "电话");
        Label washClassLabel = new Label(7, 0, "意向班级");
        Label linkmanLabel = new Label(8, 0, "学校联系人");
        Label saleLabel = new Label(9, 0, "销售人员");
        Label currentSaleLabel = new Label(10, 0, "跟进人员");
        Label clientTotalLabel = new Label(11, 0, "潜在学员");
        Label workTotalLabel = new Label(12, 0, "实训次数");
        Label speechTotalLabel = new Label(13, 0, "演讲次数");
        Label regStuLabel = new Label(14, 0, "正式学员");
        Label prevTimeLabel = new Label(15, 0, "上次跟踪时间");
        Label nextTimeLabel = new Label(16, 0, "下次跟踪时间");
        Label followStateLabel = new Label(17, 0, "跟踪状态");
        Label handsLabel = new Label(18, 0, "是否合作");
        Label stateLabel = new Label(19, 0, "签约");

        sheet.addCell(idLabel);
        sheet.addCell(nameLabel);
        sheet.addCell(shcLabel);
        sheet.addCell(addressLabel);
        sheet.addCell(improtanceLabel);
        sheet.addCell(washLabel);
        sheet.addCell(telLabel);
        sheet.addCell(washClassLabel);
        sheet.addCell(linkmanLabel);
        sheet.addCell(saleLabel);
        sheet.addCell(currentSaleLabel);
        sheet.addCell(clientTotalLabel);
        sheet.addCell(workTotalLabel);
        sheet.addCell(speechTotalLabel);
        sheet.addCell(regStuLabel);
        sheet.addCell(prevTimeLabel);
        sheet.addCell(nextTimeLabel);
        sheet.addCell(followStateLabel);
        sheet.addCell(handsLabel);
        sheet.addCell(stateLabel);

        for (int i = 0; i < rows.size(); i++) {
            School s = rows.get(i);

            int row = i + 1;

            Label sidLabel = new Label(0, row, s.getId().toString());
            Label snameLabel = new Label(1, row, s.getName());
            Label sshcLabel = new Label(2, row, s.getShc().getName());
            Label saddressLabel = new Label(3, row, s.getAddress());
            Label simprotanceLabel = new Label(4, row, s.getImprotance().getName());
            Label swashLabel = new Label(5, row, s.getWash().getName());
            Label stelLabel = new Label(6, row, s.getTel());
            Label swashClassLabel = new Label(7, row, s.getWashClass().getName());
            Label slinkmanLabel = new Label(8, row, s.getLinkman().getName());
            Label ssaleLabel = new Label(9, row, s.getSale().getRealName());
            Label scurrentSaleLabel = new Label(10, row, s.getCurrentSale().getRealName());
            Label sclientTotalLabel = new Label(11, row, "" + s.getClientTotal());
            Label sworkTotalLabel = new Label(12, row, "" + s.getWorkTotal());
            Label sspeechTotalLabel = new Label(13, row, "" + s.getSpeechTotal());
            Label sregStuLabel = new Label(14, row, "" + s.getRegStu());
            Label sprevTimeLabel = new Label(15, row, s.getPrevTime() != null ? s.getPrevTime().toLocaleString() : null);
            Label snextTimeLabel = new Label(16, row, s.getNextTime() != null ? s.getNextTime().toLocaleString() : null);
            Label sfollowStateLabel = new Label(17, row, s.getFollowStateString());
            Label shandsLabel = new Label(18, row, s.isHands() ? "合作" : "没有合作");
            Label sstateLabel = new Label(19, row, s.isState() ? "已签约" : "未签约");

            sheet.addCell(sidLabel);
            sheet.addCell(snameLabel);
            sheet.addCell(sshcLabel);
            sheet.addCell(saddressLabel);
            sheet.addCell(simprotanceLabel);
            sheet.addCell(swashLabel);
            sheet.addCell(stelLabel);
            sheet.addCell(swashClassLabel);
            sheet.addCell(slinkmanLabel);
            sheet.addCell(ssaleLabel);
            sheet.addCell(scurrentSaleLabel);
            sheet.addCell(sclientTotalLabel);
            sheet.addCell(sworkTotalLabel);
            sheet.addCell(sspeechTotalLabel);
            sheet.addCell(sregStuLabel);
            sheet.addCell(sprevTimeLabel);
            sheet.addCell(snextTimeLabel);
            sheet.addCell(sfollowStateLabel);
            sheet.addCell(shandsLabel);
            sheet.addCell(sstateLabel);
        }
        workbook.write();
        workbook.close();
    }
}
